package cn.jilian.meta.web;

import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jilian.entity.Meta;
import cn.jilian.meta.service.ExtJsGenerator;
import cn.jilian.meta.service.MetaService;

@Controller
@RequestMapping("/extjs6/")
public class ExtController {
	
	@Autowired
	private ExtJsGenerator extjsGenerator;
	@Autowired
	private MetaService metaService;
	
	@Value("${app.context.path}")
	private String contextPath;
	
	
	@GetMapping("/model/{metaId}")
	public ResponseEntity<byte[]> genModelDefineExtJs6(@PathVariable("metaId")Long metaId){
		Meta meta=metaService.getMetaById(metaId);
		String content=extjsGenerator.genModel(meta);
		try{
			String name=StringUtils.substringAfterLast(meta.getCode(), ".");
			name=StringUtils.capitalize(name);
    		 HttpHeaders headers = new HttpHeaders();
             headers.add("Content-Disposition", "attachment;filename="+name+".js");
             HttpStatus statusCode = HttpStatus.OK;
             byte[] body=content.getBytes();
             ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
             return entity;
    	}catch(Exception e){
    		e.printStackTrace();
    		return null;
    	}
	}
	
	@GetMapping("/gridpanel/{metaId}")
	public ResponseEntity<byte[]> genGridDefineByExtjs6(@PathVariable("metaId")Long metaId){
		Meta meta=metaService.getMetaById(metaId);
		String content=extjsGenerator.genGrid(meta);
		try{
			String name=StringUtils.substringAfterLast(meta.getCode(), ".");
			name=StringUtils.capitalize(name);
    		 HttpHeaders headers = new HttpHeaders();
             headers.add("Content-Disposition", "attachment;filename="+name+".js");
             HttpStatus statusCode = HttpStatus.OK;
             byte[] body=content.getBytes();
             ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
             return entity;
    	}catch(Exception e){
    		e.printStackTrace();
    		return null;
    	}
	}
	
	@GetMapping("/editorGrid/{metaId}")
	public ResponseEntity<byte[]> genEditorGridDefineByExtjs6(@PathVariable("metaId")Long metaId){
		Meta meta=metaService.getMetaById(metaId);
		String content=extjsGenerator.genGrid(meta);
		try{
			String name=StringUtils.substringAfterLast(meta.getCode(), ".");
			name=StringUtils.capitalize(name);
    		 HttpHeaders headers = new HttpHeaders();
             headers.add("Content-Disposition", "attachment;filename="+name+".js");
             HttpStatus statusCode = HttpStatus.OK;
             byte[] body=content.getBytes();
             ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(body, headers, statusCode);
             return entity;
    	}catch(Exception e){
    		e.printStackTrace();
    		return null;
    	}
	}
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@GetMapping("/icons/{id}")
	@ResponseBody
	public Object icon(@PathVariable("id")Long id) {
		String sql="select * from bt_icon where rank=?";
		return jdbcTemplate.queryForList(sql,id);
	}
	
	@GetMapping("/ux/classic.json")
	public Object getCustomComponentDefine() {
		return null;
	}
	
	@GetMapping("/conf_view/{metaId}")
	public ModelAndView menuView(@PathVariable("metaId")Long metaId,@RequestParam(name="id",required=false)Long id) {
		return getModelAndView(metaId,id,null);
	}
	
	@GetMapping("/search_grid/{metaId}")
	public ModelAndView searchGrid(@PathVariable("metaId")Long metaId,@RequestParam(name="id",required=false)Long id) {
		return getModelAndView(metaId,id,"ui_tpl.search_grid");
	}
	
	@GetMapping("/search_editgrid/{metaId}")
	public ModelAndView searchEditGrid(@PathVariable("metaId")Long metaId,@RequestParam(name="id",required=false)Long id) {
		return getModelAndView(metaId,id,"ui_tpl.search_edit_grid");
	}
	
	@GetMapping("/tree_form/{metaId}")
	public ModelAndView treeFormView(@PathVariable("metaId")Long metaId,@RequestParam(name="id",required=false)Long id) {
		return getModelAndView(metaId,id,"ui_tpl.tree_form");
	}
	@GetMapping("/tree_tab/{metaId}")
	public ModelAndView treeTabiew(@PathVariable("metaId")Long metaId,@RequestParam(name="id",required=false)Long id) {
		return getModelAndView(metaId,id,"ui_tpl.tree_tab");
	}
	
	@GetMapping("/grid_form/{metaId}")
	public ModelAndView gridFormView(@PathVariable("metaId")Long metaId,@RequestParam(name="id",required=false)Long id) {
		return getModelAndView(metaId,id,"ui_tpl.grid_form");
	}
	
	@GetMapping("/grid_attr/{metaId}")
	public ModelAndView gridPropsView(@PathVariable("metaId")Long metaId,@RequestParam(name="id",required=false)Long id) {
		return getModelAndView(metaId,id,"ui_tpl.grid_attr");
	}
	
	@GetMapping("/grid_fieldset/{metaId}")
	public ModelAndView gridFieldset(@PathVariable("metaId")Long metaId,@RequestParam(name="id",required=false)Long id) {
		return getModelAndView(metaId,id,"ui_tpl.grid_fieldset");
	}
	
	@GetMapping("/single_form/{metaId}")
	public ModelAndView single_form(@PathVariable("metaId")Long metaId,@RequestParam(name="id",required=false)Long id) {
		return getModelAndView(metaId,id,"ui_tpl.single_form");
	}
	
	private ModelAndView getModelAndView(Long metaId,Long id,String jsfile) {
		ModelAndView mv=new ModelAndView("extjs6/tab");
		Map<String,Object> metaData=metaService.getFullMetaById(metaId);
		mv.addObject("metaData", metaData);
		mv.addObject("contextPath",contextPath);
		mv.addObject("jsfile",jsfile );
		if(id!=null) {
			mv.addObject("id", id);
		}
		return mv;
	}
	
	
	
	

}
